﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;

namespace SystemContainer.Models
{
    /*
     * all raw data in database
     * */
    public class SystemEntities : DbContext
    {
       public DbSet<Field> Fields { get; set; }
       public DbSet<Algorithm> Algorithms { get; set; }
       public DbSet<CheckInData> CheckIns { get; set; }
       public DbSet<PhotoData> PhotoDatas { get; set; }
       public DbSet<Photo> Photos { get; set; }
       public DbSet<Combination> list { get; set; }     

        public DbSet<Venue> Venues { get; set; }
        public DbSet<ScenicLocation> ScenicLocations { get; set; }
        public DbSet<City> Cities { get; set; }
        public DbSet<AreaOfInterest> AreasOfInterests { get; set; }


         protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
           /* modelBuilder.Entity<Algorithm>()
                .HasMany<DataSet>(a => a.datas)
                .WithMany(p => p.als)
                .Map(
                     m =>
                     {
                         m.MapLeftKey("datasetId");
                         m.MapRightKey("algorithmId");
                         m.ToTable("DatasetAlgorithm");
                     }
                );*/
             
             //one city mapped with many AOIs
           // modelBuilder.Entity<City>().HasMany(c => c.AreaOfInterests).WithRequired(a => a.city);
            modelBuilder.Entity<AreaOfInterest>().HasRequired(a => a.city).WithMany(c => c.AreaOfInterests).HasForeignKey(a=>a.CityID);
            //modelBuilder.Entity<City>().HasMany(c => c.Venues).WithRequired(v =>v.city);
            //modelBuilder.Entity<City>().HasMany(c => c.ScenicLocations).WithRequired(s => s.city);
            //modelBuilder.Entity<City>().HasMany(c => c.Photos).WithRequired(p => p.city);


             //one scenicLocation mapped with many photos
            modelBuilder.Entity<ScenicLocation>().HasMany(s => s.photos).WithRequired(p => p.scenicLocation).HasForeignKey(p => new { p.scenicLocationID, p.ScecCityID });
           // modelBuilder.Entity<Photo>().HasRequired(p => p.scenicLocation).WithMany(s => s.photos).HasForeignKey(p => new { p.scenicLocationID, p.ScecCityID });
     
             //one AOI mapped with many sceniclocations
            modelBuilder.Entity<AreaOfInterest>().HasMany(a => a.ScenicLocations).WithRequired(s =>s.AOI).HasForeignKey(s => new { s.AreaOfInterestID, s.AOICityID });
           // modelBuilder.Entity<ScenicLocation>().HasRequired(s => s.AOI).WithMany(a => a.ScenicLocations).HasForeignKey(s => new { s.AreaOfInterestID, s.AOICityID });
             
            //one AOI mapped with many venues
            modelBuilder.Entity<AreaOfInterest>().HasMany(a => a.Venues).WithRequired(v => v.AOI).HasForeignKey(v => new { v.AreaOfInterestID, v.AOICityID });
            //modelBuilder.Entity<Venue>().HasRequired(v => v.AOI).WithMany(a => a.Venues).HasForeignKey(v => new { v.AreaOfInterestID, v.AOICityID});


             
               }
    
    }


}